package com.zlx.module_base.receive;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;


/**二维码扫描广播（常驻广播）
 * @auther 刘欢
 * 目前只兼容广州检修新大陆NLS-MT90型号PDA（com.android.action.SEND_SCAN_RESULT）
 * @Data 2022/05/23
 */
public class ScanReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String code = null;
        // 获取扫描结果
        if (BroadcastActionConstants.NLS_MT90_SEND_SCAN_RESULT.equals(action)) {
            byte[] bvalue = intent.getByteArrayExtra("scan_result_one_bytes");
//            byte[] bvalue2 = intent.getByteArrayExtra("scan_result_two_bytes");
//            String svalue2 = null;

            try {
                if (bvalue != null) {
                    code = new String(bvalue, "utf-8");
                }

//                    if (bvalue2 != null) {
//                        svalue2 = new String(bvalue1, "GBK");
//                    }
                code = code == null ? "" : code;
//                    svalue2 = svalue2 == null ? "" : svalue2;
            } catch (Exception var9) {
                var9.printStackTrace();
            }
        }
        // 获取扫描结果后回调
        if (null!=code){
            if(EventBus.getDefault()!=null){
                MessageBean messageBean = new MessageBean();
                messageBean.setMsgType("scan");
                messageBean.setMsgInfo(code);
                EventBus.getDefault().post(messageBean);
            }
        } else {
            ToastUtils.showShort("扫码失败，请重试！");
        }
        Log.e("二维码：",code);
    }
}